package com.jp.knowledge.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.comm.JpApplication;
import com.jp.knowledge.model.KnowlageData;
import com.jp.knowledge.model.UserData;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.util.ImageLoadUtil;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;

import org.xutils.common.util.DensityUtil;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by jiayen on 17/6/1.
 */

public class KnowlageItemAdapter extends  JpBaseRecyViewAdapter/* implements StickyRecyclerHeadersAdapter<RecycleVHolder>*/ {

    private int coverHeight;
    private JpApplication application;

    public KnowlageItemAdapter(Context context, List data) {
        this(context, data, null);
    }

    public KnowlageItemAdapter(Context context, List data, JpApplication application) {
        super(context, data);
        this.application = application;
        this.coverHeight = (int) (DensityUtil.getScreenWidth() * 302f / 922f + 0.5f);
    }

    @Override
    public int getLayoutId() {
        return R.layout.knowlage_item;
    }

    @Override
    public void bindViewHold(JPRecycleViewHolder holder, int position) {
        KnowlageData.GroupListBean  groupListBean = (KnowlageData.GroupListBean) getItem(position);

        TextView tittle = holder.getView(R.id.title);
        TextView vip_tip = holder.getView(R.id.vip_tip);
        TextView count_knowlage = holder.getView(R.id.count_knowlage);
        TextView count_read = holder.getView(R.id.count_read);
        ImageView bg_icon = holder.getView(R.id.bg_icon);

        bg_icon.getLayoutParams().height = coverHeight;
        tittle.setText(groupListBean.getPackageTitle());
        if(groupListBean.getInfoNum() <= 0){
            count_read.setVisibility(View.GONE);
        } else{
            count_read.setVisibility(View.GONE);
            count_read.setText(groupListBean.getInfoNum()+"");
        }
        if(groupListBean.getReadNum() <= 0){
            count_knowlage.setVisibility(View.GONE);
        } else{
            count_knowlage.setVisibility(View.VISIBLE);
            count_knowlage.setText(groupListBean.getReadNum()+"");
        }
        ImageLoadUtil.load(mContext,groupListBean.getPackageCover(),bg_icon);

        String organizaName = groupListBean.getOrganizaName();
        String packageTip = groupListBean.getPackageTip();
        if(organizaName == null)
            organizaName = "";
        organizaName = organizaName.trim();
        if(packageTip == null)
            packageTip = "";
        packageTip = packageTip.trim();
        if(organizaName.length() > 0){
            vip_tip.setVisibility(View.VISIBLE);
            vip_tip.setText(organizaName);
        } else if(packageTip.length() > 0){
            vip_tip.setVisibility(View.VISIBLE);
            vip_tip.setText(packageTip);
        } else{
            vip_tip.setVisibility(View.GONE);
        }

        boolean canSee = true;
        if(application != null) {
            long startTime = groupListBean.getStartTime();
            long currentTimeMillis = System.currentTimeMillis();
            Calendar today = Calendar.getInstance();
            today.setTime(new Date(currentTimeMillis));
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date(startTime));
            boolean isVip = application.getUserData() != null && application.getUserData().getIsVip() == 1;
            if(currentTimeMillis < startTime && (!isVip || !isSameDay(today, calendar)))
                canSee = false;
        }
        tittle.setTextColor(mContext.getResources().getColor(canSee ? R.color.font_black_think : R.color.font_gray_light));

        if(position == 0 || groupListBean.getPackageNodeId() != ((KnowlageData.GroupListBean) getItem(position-1)).getPackageNodeId()){
            holder.getView(R.id.split_line_height).setVisibility(View.VISIBLE);
            holder.getView(R.id.split_header).setVisibility(View.VISIBLE);
            ImageView headerIcon = holder.getView(R.id.header_icon);
            TextView headerTitle = holder.getView(R.id.header_title);
            setHeaderView(groupListBean, headerIcon, headerTitle, canSee);
        } else{
            holder.getView(R.id.split_line_height).setVisibility(View.GONE);
            holder.getView(R.id.split_header).setVisibility(View.GONE);
        }
    }

//    @Override
//    public long getHeaderId(int position) {
//        return ((KnowlageData.GroupListBean)getData().get(position)).getPackageNodeId();
//    }
//
//    @Override
//    public RecycleVHolder onCreateHeaderViewHolder(ViewGroup parent) {
//        View view = LayoutInflater.from(parent.getContext())
//                .inflate(R.layout.header_knowledge_package, parent, false);
//        return new RecycleVHolder(view);
//    }
//
//    @Override
//    public void onBindHeaderViewHolder(RecycleVHolder holder, int position) {
//        KnowlageData.GroupListBean data = (KnowlageData.GroupListBean)getData().get(position);
//        setHeaderView(data, holder.getImageView(R.id.header_icon), holder.getTextView(R.id.header_title), true);
//    }

    private void setHeaderView(KnowlageData.GroupListBean data, ImageView headerIcon, TextView headerTitle, boolean canSee){
        switch (data.getPackageNodeId()){
            case 1: headerIcon.setImageResource(canSee ? R.mipmap.breakfast : R.mipmap.breakfast_gray);        break;
            case 2: headerIcon.setImageResource(canSee ? R.mipmap.morningtea : R.mipmap.morningtea_gray);      break;
            case 3: headerIcon.setImageResource(canSee ? R.mipmap.lunch : R.mipmap.lunch_gray);                 break;
            case 4: headerIcon.setImageResource(canSee ? R.mipmap.afternoontea : R.mipmap.afternoontea_gray); break;
            case 5: headerIcon.setImageResource(canSee ? R.mipmap.dinner : R.mipmap.dinner_gray);               break;
            case 6: headerIcon.setImageResource(canSee ? R.mipmap.supper : R.mipmap.supper_gray);               break;
            default:  headerIcon.setImageResource(canSee ? R.mipmap.dinner : R.mipmap.dinner_gray);
        }
        headerTitle.setText(data.getPackageNode());
        headerTitle.setTextColor(mContext.getResources().getColor(canSee ? R.color.theme_color : R.color.font_gray));
    }

    private boolean isSameDay(Calendar calendar1, Calendar calendar2){
        if(calendar1 == null || calendar2 == null
                || calendar1.get(Calendar.YEAR) != calendar2.get(Calendar.YEAR)
                || calendar1.get(Calendar.MONTH) != calendar2.get(Calendar.MONTH)
                || calendar1.get(Calendar.DAY_OF_MONTH) != calendar2.get(Calendar.DAY_OF_MONTH))
            return false;
        return true;
    }
}
